package com.example.fly.pushdemo;

import android.content.ComponentName;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.example.fly.pushdemo.smack.SendMsg;
import com.example.fly.pushdemo.smack.extension.MyPushService;

import xmpp.androidpush.aidl.PushBinder;
import xmpp.androidpush.aidl.PushMessage;
import xmpp.androidpush.service.PushConfig;
import xmpp.androidpush.service.PushService;

public class MainActivity extends AppCompatActivity  implements ServiceConnection {
    private SendMsg mSendMsg;
    private String deviceId;
    private Button btsend;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button btmessage = (Button) findViewById(R.id.bt_message);
        Button btsend = (Button) findViewById(R.id.bt_send);

        deviceId = "b16ae99e392823cc";
        PushConfig config = new PushConfig(BuildConfig.XMPP, BuildConfig.XMPP_PORT, deviceId, "123456");
        PushService.startService(this.getApplicationContext(), MyPushService.class, config);
        PushService.bindService(this.getApplicationContext(), MyPushService.class,this);
        btsend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bindXmpp();
            }
        });
        btmessage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mSendMsg = new SendMsg(deviceId,mPushBinder);
                if(mPushBinder!=null&&mPushBinder.isConnected()){
                    PushMessage pushMessage =new PushMessage("from","to","msgmsgmsg");
                    mPushBinder.sendMessage(pushMessage);
                }
            }
        });
    }

    private void bindXmpp() {
        mSendMsg = new SendMsg(deviceId,mPushBinder);
        if(mPushBinder!=null&&mPushBinder.isConnected()){
            mSendMsg.sendTokenMsg("617d2dfabdfc6fd8dc29c77b4dff1e1178ce0a38", "25104847426551852");
        }else {
            Toast.makeText(this, "mPushBinder", Toast.LENGTH_SHORT).show();
        }


    }
    private PushBinder mPushBinder;
    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        mPushBinder=new PushBinder(service);
    }

    @Override
    public void onServiceDisconnected(ComponentName name) {
        mPushBinder=null;
    }
}
